﻿Public Module Functions

    'Public Sub FillComboBox(ByRef cbo As ComboBox, ByVal dt As DataTable)

    '    dt.Rows.InsertAt(dt.NewRow(), 0)

    '    cbo.DataSource = dt

    '    cbo.DisplayMember = dt.Columns.Item(1).ColumnName

    '    cbo.ValueMember = dt.Columns.Item(0).ColumnName

    'End Sub


    ''' <summary>
    ''' convert string database month yyyyMM to MM/yyyy
    ''' </summary>
    ''' <param name="strMonth"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ConvertStrDBMonthToStrMonth(ByVal strMonth As String) As String

        Return strMonth.Substring(4, 2) & "/" & strMonth.Substring(0, 4)

    End Function

    ''' <summary>
    ''' convert string month MM/yyyy to string database month yyyyMM
    ''' </summary>
    ''' <param name="strMonth"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ConvertStrMonthToStrDBMonth(ByVal strMonth As String) As String

        Dim str() As String = strMonth.Split("/")

        Return str(1) & str(0)

    End Function

    ''' <summary>
    ''' validate string month MM/yyyy
    ''' </summary>
    ''' <param name="strMonth"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ValidateStrMonth(ByVal strMonth As String, ByVal yearFrom As Integer, ByVal yearTo As Integer) As Boolean

        Dim str() As String = strMonth.Split("/")

        ' get month
        Dim month As Integer = Integer.Parse(str(0))


        ' check month
        If month >= 1 And month <= 12 Then

            ' get year
            Dim year As Integer = Integer.Parse(str(1))

            ' check year > yearFrom and < yearTo
            If year > yearFrom And year < yearTo Then

                Return True

            Else

                Return False

            End If

        Else

            Return False

        End If



    End Function

    ''' <summary>
    ''' Function valid string format date dd/MM/yyyy
    ''' </summary>
    ''' <param name="strDate">string date</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ValidateStrDate(ByVal strDate As String, ByVal yearFrom As Integer, ByVal yearTo As Integer) As Boolean

        Dim str() As String = strDate.Split("/")

        ' get month from string date
        Dim month As Integer = Integer.Parse(str(1))

        ' validate month
        If month >= 1 And month <= 12 Then

            ' get year from string date
            Dim year As Integer = Integer.Parse(str(2))

            ' check year > yearFrom and < yearTo
            If year > yearFrom And year < yearTo Then

                'get day from string date
                Dim day As Integer = Integer.Parse(str(0))

                ' get number of days in month/year
                Dim numOfDays As Integer = Date.DaysInMonth(year, month)

                'validate day
                If day > 0 And day <= numOfDays Then

                    Return True
                Else

                    Return False

                End If
            Else

                Return False

            End If

        Else

            Return False

        End If

    End Function


    ''' <summary>
    ''' Function convert string format date dd/MM/yyyy to integer
    ''' </summary>
    ''' <param name="strDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertStrDateToInt(ByVal strDate As String) As Integer

        Dim str() As String = strDate.Split("/")

        Return Integer.Parse(str(0) & str(1) & str(2))

    End Function


    ''' <summary>
    ''' Function convert string hour format hh:mm to integer 
    ''' </summary>
    ''' <param name="strHour"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertStrHourToInt(ByVal strHour As String) As Integer

        'If Not strHour Is Nothing Then

        Dim str() As String = strHour.Split(":")

        Return Integer.Parse(str(0) & str(1))

        'Else

        ' Return Nothing
        '
        ' End If

    End Function

    Public Function ValidateStrHour(ByVal strHour As String) As Boolean

        Dim str() As String = strHour.Split(":")

        ' get hour format integer from string hour
        Dim hour As Integer = Integer.Parse(str(0))

        'validate hour
        If hour >= 0 And hour < 24 Then

            ' get minnute format integer from string hour
            Dim minute As Integer = Integer.Parse(str(1))

            ' validate minute
            If minute >= 0 And minute < 60 Then

                Return True

            Else

                Return False

            End If

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' convert string db date yyyyMMdd to dd/MM/yyyy
    ''' </summary>
    ''' <param name="strDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertstrDBDateToStrDate(ByVal strDBDate As String) As String

        Return strDBDate.Substring(6, 2) & "/" & strDBDate.Substring(4, 2) & "/" & strDBDate.Substring(0, 4)

    End Function

    ''' <summary>
    ''' convert string dd/MM/yyyy to string db date yyyyMMdd
    ''' </summary>
    ''' <param name="strDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertStrDateToStrDBDate(ByVal strDate As String) As String

        Dim strings As String() = strDate.Split("/")
        ' yyyyMMdd
        Return strings(2) & strings(1) & strings(0)

    End Function

    ''' <summary>
    '''  convert string MM/yyyy to integer
    ''' </summary>
    ''' <param name="strMonth"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertStrMonthToInt(ByVal strMonth As String) As Integer

        Dim str() As String = strMonth.Split("/")

        Return Integer.Parse(str(0) & str(1))

    End Function

    ''' <summary>
    ''' subtract hour format integer hhmm to minute format integer mm.
    ''' Return hour format integer hhmm
    ''' </summary>
    ''' <param name="hours"></param>
    ''' <param name="minutes"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SubtractHourToMinute(ByVal hours As Integer, ByVal minutes As Integer) As Integer

        hours = ((hours \ 100) * 60 + (hours Mod 100)) - minutes

        Return (hours \ 60) * 100 + (hours Mod 60)

    End Function


    ''' <summary>
    ''' subtract hour hhmm to hour hhmm. Return minutes integer
    ''' </summary>
    ''' <param name="h1"></param>
    ''' <param name="h2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SubtractHourToHour(ByVal h1 As Integer, ByVal h2 As Integer) As Integer

        h1 = ((h1 \ 100) * 60 + (h1 Mod 100)) - ((h2 \ 100) * 60 + (h2 Mod 100))

        Return h1

    End Function

    ''' <summary>
    ''' add hour hhmm return hour hhmm
    ''' </summary>
    ''' <param name="h1"></param>
    ''' <param name="h2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddHour(ByVal h1 As Integer, ByVal h2 As Integer) As Integer

        h1 = ((h1 \ 100) * 60 + (h1 Mod 100)) + ((h2 \ 100) * 60 + (h2 Mod 100))

        Return (h1 \ 60) * 100 + (h1 Mod 60)

    End Function

    ''' <summary>
    ''' subtract hour to hour return hour hhmm
    ''' </summary>
    ''' <param name="h1"></param>
    ''' <param name="h2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SubtractHourReturnHour(ByVal h1 As Integer, ByVal h2 As Integer) As Integer

        h1 = ((h1 \ 100) * 60 + (h1 Mod 100)) - ((h2 \ 100) * 60 + (h2 Mod 100))

        Return (h1 \ 60) * 100 + (h1 Mod 60)

    End Function

    ''' <summary>
    ''' add hours hhmm and minutes mm return hours hhmm
    ''' </summary>
    ''' <param name="hours"></param>
    ''' <param name="minutes"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddHoursAndMinutes(ByVal hours As Integer, ByVal minutes As Integer) As Integer

        hours = ((hours \ 100) * 60 + (hours Mod 100)) + minutes

        Return (hours \ 60) * 100 + (hours Mod 60)

    End Function

    ''' <summary>
    ''' convert minutes in DB to hour format hh:mm
    ''' </summary>
    ''' <param name="minutes"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ConvertMinutesToHours(ByVal minutes As Integer) As String

        Dim hours As String = (minutes \ 60).ToString("00") + ":" + (minutes Mod 60).ToString("00")

        Return hours

    End Function

    ''' <summary>
    ''' Convert hour to minutes
    ''' </summary>
    ''' <param name="hours"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ConvertHourToMinutes(ByVal hours As Integer) As Integer

        '  MsgBox(((hours \ 100) * 60) + (hours Mod 100))
        Return ((hours \ 100) * 60) + (hours Mod 100)

    End Function


End Module
